package com.weetop.cfw.home_page.fragment

import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.WorkshopWarehouseKindView
import com.weetop.cfw.base.presenter.imp.WorkshopWarehouseKindPresenterImp
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.bean.HotSitesBean
import com.weetop.cfw.bean.WorkshopWarehouseListBean
import com.weetop.cfw.constants.Constants
import com.weetop.cfw.event.*
import com.weetop.cfw.kind.activity.WorkshopDetailActivity
import com.weetop.cfw.mine.adapter.WorkshopBrowseRecordAdapter
import kotlinx.android.synthetic.main.fragment_workshop_warehouse_kind.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 * 厂房仓库列表界面
 */
class WorkshopWarehouseKindFragment: CommonBaseFragment(), WorkshopWarehouseKindView,OnRefreshLoadMoreListener {

    private lateinit var workshopBrowseRecordAdapter:WorkshopBrowseRecordAdapter

    private lateinit var workshopWarehouseKindPresenterImp:WorkshopWarehouseKindPresenterImp

    private val  workshopWarehouseList  = ArrayList<WorkshopWarehouseListBean.DataBean>()

    private var intId:String = "1"//意向ID，出租为1，出售为2
    private var typeId:String = "1201"//类型ID，厂库为1201，仓库为1202
    private var areaId:String = "0"//区域ID
    private var acrId:String = "0"//面积ID
    private var pucateId:String = "0"//价格区间
    private var vocId:String = "0"//行业ID
    private var utype:String = "0"//来源ID
    private var sort:String = "0"//默认排序
    private var currentPageNumber:Int = 1//当前数据的页数
    private var totalCount:Int = 0//数据的总记录数

    private var isRefresh = false//是否正在刷新
    private var isLoadMore = false//是否加载更多

    var workshopWarehouseType = 0//厂房仓库种类 0:厂房土地出租 1:厂房土地出售 2:仓库出租 3:仓库出售

    companion object{
        fun getInstance(workshopWarehouseType:Int):WorkshopWarehouseKindFragment{
            val workshopWarehouseKindFragment = WorkshopWarehouseKindFragment()
            workshopWarehouseKindFragment.workshopWarehouseType = workshopWarehouseType
            return workshopWarehouseKindFragment
        }
    }

    override fun getLayoutId() = R.layout.fragment_workshop_warehouse_kind

    override fun initView() {
        workshopWarehouseKindPresenterImp = WorkshopWarehouseKindPresenterImp()
        workshopWarehouseKindPresenterImp.attachView(this)

        workshopWarehouseKindRefreshLayout.setOnRefreshLoadMoreListener(this)
        workshopWarehouseKindRefreshLayout.setEnableAutoLoadMore(false)
    }

    override fun initData() {
        workshopBrowseRecordAdapter = WorkshopBrowseRecordAdapter(R.layout.layout_item_workshop_browse_record,workshopWarehouseList)
        workshopBrowseRecordAdapter.emptyView = emptyView
        workshopBrowseRecordAdapter.setOnItemClickListener { _, _, position ->
//                if(LogInUtils.userIsLogIn()){
                    when (workshopWarehouseType) {
                        0 -> {//厂房土地出租
//                            if(LogInUtils.userIsLogIn()){
                                WorkshopDetailActivity.startWorkshopDetailActivity(context!!,workshopWarehouseList[position].infoid,2)
//                            }
                        }
                        1 -> {//厂房土地出售
//                            if(LogInUtils.userIsLogIn()){
                                WorkshopDetailActivity.startWorkshopDetailActivity(context!!,workshopWarehouseList[position].infoid,1)
//                            }
                        }
                        2 -> {//仓库出租
//                            if(LogInUtils.userIsLogIn()){
                                WorkshopDetailActivity.startWorkshopDetailActivity(context!!,workshopWarehouseList[position].infoid,4)
//                            }
                        }
                        3 -> {//仓库出售
//                            if(LogInUtils.userIsLogIn()){
                                WorkshopDetailActivity.startWorkshopDetailActivity(context!!,workshopWarehouseList[position].infoid,2)
//                            }
                        }
                    }
//                }
        }
        workshopWarehouseKindRV.adapter = workshopBrowseRecordAdapter

        typeId = if(workshopWarehouseType == 0 || workshopWarehouseType == 1){
            "1201"
        }else{
            "1202"
        }

        intId = if(workshopWarehouseType == 0 || workshopWarehouseType == 2){
            "1"
        }else{
            "2"
        }

        if(workshopWarehouseType == 0){
            workshopWarehouseKindRefreshLayout.autoRefresh()
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    fun onReceiveKindTabSelectedEvent(kindTabSelectedEvent:KindTabSelectedEvent){

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceiveHotSitesBean(hotSitesDataBean:HotSitesBean.DataBean){
        workshopWarehouseKindRefreshLayout.autoRefresh()
    }


    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    fun onReceiveMianJiFilterEvent(mianJiFilterEvent: MianJiFilterEvent){
        if(mianJiFilterEvent.position == workshopWarehouseType){
            acrId = if(mianJiFilterEvent.unselectedState){
                "0"
            }else{
                mianJiFilterEvent.mianJiFilterBean.codeid.toString()
            }
            workshopWarehouseKindRefreshLayout.autoRefresh()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    fun onReceiveAreaFilterEvent(areaFilterEvent:AreaFilterEvent){
        if(areaFilterEvent.position == workshopWarehouseType){
            areaId = if(areaFilterEvent.unselectedState){
                "0"
            }else{
                areaFilterEvent.areaFilterBean.areaid.toString()
            }
            workshopWarehouseKindRefreshLayout.autoRefresh()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    fun onReceivePriceFilterEvent(priceFilterEvent:PriceFilterEvent){
        if(priceFilterEvent.position == workshopWarehouseType){
            pucateId = if(priceFilterEvent.unselectedState){
                "0"
            }else{
                priceFilterEvent.priceFilterBean.codeid.toString()
            }
            workshopWarehouseKindRefreshLayout.autoRefresh()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    fun onReceiveIndustryFilterEvent(industryFilterEvent:IndustryFilterEvent){
        if(industryFilterEvent.position == workshopWarehouseType){
            vocId = if(industryFilterEvent.unselectedState){
                "0"
            }else{
                industryFilterEvent.industryFilterBean.codeid.toString()
            }
            workshopWarehouseKindRefreshLayout.autoRefresh()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    fun onReceiveSourceFilterEvent(sourceFilterEvent:SourceFilterEvent){
        if(sourceFilterEvent.position == workshopWarehouseType){
            utype = if(sourceFilterEvent.unselectedState){
                "0"
            }else{
                sourceFilterEvent.sourceFilterBean.codeid.toString()
            }
            workshopWarehouseKindRefreshLayout.autoRefresh()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        workshopWarehouseKindPresenterImp.detachView()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        if(isFirstLoadData){
            val queryParamMap = mapOf(
                "siteid" to Constants.siteId.toString(),
                "intid" to intId,
                "typeid" to typeId,
                "areaid" to areaId,
                "acrid" to acrId,
                "pucate" to pucateId,
                "vocid" to vocId,
                "utype" to utype,
                "sort" to sort,
                "curpage" to currentPageNumber.toString(),
                "pagesize" to "10"
            )
            workshopWarehouseKindPresenterImp.getWorkshopWarehouseListData(this,queryParamMap)
        }
    }
    /**
     * 厂房列表数据获取成功
     */
    override fun workshopWarehouseListDataGetSuccess(workshopWarehouseListBean: WorkshopWarehouseListBean?) {
        if(workshopWarehouseListBean != null){

            totalCount = workshopWarehouseListBean.records

            isFirstLoadData = false
            if(isRefresh){//处于刷新状态
                workshopWarehouseList.clear()
                workshopWarehouseList.addAll(workshopWarehouseListBean.data)
                workshopBrowseRecordAdapter.notifyDataSetChanged()
                workshopWarehouseKindRefreshLayout.finishRefresh(true)
                if(workshopWarehouseListBean.data.size > 0){
                    currentPageNumber = 1//将当前数据页数重置为1
                }
                isRefresh = false
                return
            }
            if(isLoadMore){
                workshopWarehouseList.addAll(workshopWarehouseListBean.data)
                workshopBrowseRecordAdapter.notifyDataSetChanged()
                workshopWarehouseKindRefreshLayout.finishLoadMore(true)
                if(workshopWarehouseListBean.data.size > 0){
                    currentPageNumber += 1//将当前数据页数增加1
                }
                isLoadMore = false
                return
            }
            workshopWarehouseList.addAll(workshopWarehouseListBean.data)
            workshopBrowseRecordAdapter.notifyDataSetChanged()
        }
    }

    /**
     * 厂房列表数据获取失败
     */
    override fun workshopWarehouseListDataGetFailed() {
        workshopWarehouseKindRefreshLayout.finishLoadMore(false)
        workshopWarehouseKindRefreshLayout.finishRefresh(false)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if(totalCount <= workshopWarehouseList.size){
            showNativeShortToast("暂无更多数据")
            workshopWarehouseKindRefreshLayout.finishLoadMoreWithNoMoreData()
        }else{
            isLoadMore = true
            val tempQueryMap = mapOf(
                "siteid" to Constants.siteId.toString(),
                "intid" to intId,
                "typeid" to typeId,
                "areaid" to areaId,
                "acrid" to acrId,
                "pucate" to pucateId,
                "vocid" to vocId,
                "utype" to utype,
                "sort" to sort,
                "curpage" to ((currentPageNumber + 1).toString()),
                "pagesize" to "10"
            )
            workshopWarehouseKindPresenterImp.getWorkshopWarehouseListData(this,tempQueryMap)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isRefresh = true
        val tempQueryMap = mapOf(
            "siteid" to Constants.siteId.toString(),
            "intid" to intId,
            "typeid" to typeId,
            "areaid" to areaId,
            "acrid" to acrId,
            "pucate" to pucateId,
            "vocid" to vocId,
            "utype" to utype,
            "sort" to sort,
            "curpage" to "1",
            "pagesize" to "10"
        )
        workshopWarehouseKindPresenterImp.getWorkshopWarehouseListData(this,tempQueryMap)
    }

}